<?php
/**
*common function 
*/

/**
 * Determines if the current version of PHP is greater then the supplied value
 *@param string
 *@return bool
 */
if( ! function_exists('is_php')) {
	function is_php($version = '5.0.0') 
	{
		static $_is_php;
		$version = (string)$version;
		
		if( ! isset($_is_php[$version])) {
			$_is_php[$version] = (version_compare(PHP_VERSION, $version) > 0) ? TRUE : FALSE;
				
		}
		return $_is_php[$version];
	}
}
//------------------------------------------------------------------------------------------------
/**
*Tests for file writability
*is_writable 返回 TRUE 在 Windows 下判断一个不能写的文件，因为设置了只读属性，但是在Unix下如果开启
*了safe_mode 安全模式 is_writable是不可靠的。
*/
if( ! function_exists('is_really_writable')) {
	function is_really_writable($file) 
	{
		//If we're on a Unix server with safa_mode off we call is_writable
		if(DIRECTORY_SEPARATOR == '/' and @ini_get("safa_mode") == FALSE) {
			return is_writable($file);
		}
		//for windows servers and safe_mode "on" installations we'll actually
		// write a file then read it
		if(is_dir($file)) {
			$file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));

			if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
			{
				return FALSE;
			}

			fclose($fp);
			@chmod($file, DIR_WRITE_MODE);
			@unlink($file);
			return TRUE;
		} elseif( ! is_file($file) or ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) {
			return FALSE;
		}
	
		fclose($fp);
	}
}

/**
* class registry 函数注册
* 将函数对象放进一个静态变量，存在返回，不存在则加入
* 
*
*/
if( ! function_exists('load_class')) {
	function load_class($class, $directory = 'libraries', $prefix = 'CI_') {
		static $_classes = array();
		
		//Does the class exist? if so we're done
		if(isset($_classes[$class])) {
			return $_classes[$class];
		}
		
		$name = FALSE;
		foreach(array(APPPATH, BASEPATH) as $path) {
			if(file_exists($path.$directory.'/'.$class.'.php')) {
				$name = $prefix.$class;
				
				if(class_exists($name) === FALSE) {
					require($path.$directory.'/'.$class.'.php');
				}
				break;
			}
		}
		//如果请是扩展类 我们也加载
		if(file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')) {
			$name = config_item('subclass_prefix').$class;
			
			if(class_exists($name) === FALSE) {
				require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')
			}
		}
		//还没有发现这个类
		if($name === FALSE) {
			exit('Unable to locate the specified class:'.$class.'.php');
		}
		
		// keey track of what we just loaded
		is_loaded($class);
		
		$_classes[$class] = new $name;
		
		$return $_classes[$class];
	}
}
/**
*记录加载过的库
*/
if( ! function_exists('is_loaded')) {
	function &is_loaded($class = '') 
	{
		static $_is_loaded = array();
		
		if($class != '') {
			$_is_loaded[strtolower($class)] = $class;
		}
		return $_is_loaded;
	}
}

/**
*加载主要的配置文件
*/
if( ! function_exists('get_config')) {
	function &get_config($replace = array()) 
	{
		static $_config;
		if (is_set($_config)){
			return $_config[0];
		}
		
		//不用的环境加载不同的配置文件
		$file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'
			
		if( ! define('ENVIRONMENT') || ! file_exists($file_path)) {
			$file_path = APPPATH.'config/config.php';
		}
		
		//Fetch the config file
		if( ! file_exists($file_path)) {
			exit('The configuration file does not exist');
		}
		
		require($file_path);
		
		//配置文件是否是一个数组
		if( ! isset($config) || !is_array($config)) {
			exit('Your config file does not appear to be formatted correctly');
		}
		
		//动态的加载配置
		if (count($replace) > 0) {
			foreach ($replace as $key => $val) {
				if(isset($config[$key])) {
					$config[$key] = $val;
				}
			}
		}
		
		$_config[0] = & $config;
		return $_config[0];
	}
}

/**
*返回指定的配置
*/
if( ! function_exists('config_item')) {
	function config_item($item) 
	{
		static $_config_item = array();
		if( ! isset($_config_item[$item])) {
			$config = &get_config();
			if( ! isset($config[$item])) {
				return FALSE;
			}
			$_config_item[$item] = $config[$item];
		}
		return $_config_item[$item];
	}
}














